using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ArrowLauncher : MonoBehaviour 
{
	public int Delay;
	public Vector3 ArrowSpeed;
	public GameObject Arrow;
	public int ArrowCount;
	
	private float PreviousLaunchTime;
	private List<GameObject> ArrowsList;
	
	// Use this for initialization
	void Start () 
	{
		PreviousLaunchTime = Time.time;
		ArrowsList = new List<GameObject>();
		
		for(int i = 0; i < ArrowCount; ++i)
		{
			GameObject NewArrow = (GameObject)Instantiate(Arrow);
			NewArrow.SetActive(false);
			ArrowsList.Add(NewArrow);
		}
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(Time.time >= PreviousLaunchTime + Delay)
		{
			//spawn
			//Debug.Log("Spawn");
			launch();
			PreviousLaunchTime = Time.time;
		}
	}
	
	private void launch()
	{
		foreach(GameObject go in ArrowsList)
		{
			if(go.activeInHierarchy)
				continue;
			
			Arrow cArrow = go.GetComponent<Arrow>();
			cArrow.Speed = ArrowSpeed;
			cArrow.transform.position = transform.position;
			go.SetActive(true);
				
		}		
	}
}
